package com.devenv.entity;

import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.time.LocalDateTime;

/**
 * 镜像源实体类
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Entity
@Table(name = "mirror_source")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class MirrorSource {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "software_id", nullable = false)
    private Long softwareId;

    @Column(name = "name", nullable = false, length = 100)
    private String name;

    @Column(name = "base_url", nullable = false, length = 500)
    private String baseUrl;

    @Column(name = "region", nullable = false, length = 10)
    private String region;

    @Column(name = "priority")
    private Integer priority = 0;

    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false)
    private Status status = Status.ACTIVE;

    @Column(name = "last_check_time")
    private LocalDateTime lastCheckTime;

    @Column(name = "speed_score", precision = 3, scale = 2)
    private java.math.BigDecimal speedScore;

    @Column(name = "reliability_score", precision = 3, scale = 2)
    private java.math.BigDecimal reliabilityScore;

    @CreationTimestamp
    @Column(name = "created_at")
    private LocalDateTime createdAt;

    @UpdateTimestamp
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    // 关联关系
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "software_id", insertable = false, updatable = false)
    private Software software;

    // 状态枚举
    public enum Status {
        ACTIVE, INACTIVE, ERROR
    }

    // 便利方法：检查是否活跃
    public boolean isActive() {
        return Status.ACTIVE.equals(this.status);
    }
}
